﻿using Crossroad.Core.Configs;
using Crossroad.Core.Models;
using Crossroad.OAuth.Attributes;
using Crossroad.System.Admin.Attributes;
using Crossroad.System.Admin.Dto;
using Crossroad.System.Admin.Model;
using Crossroad.System.Admin.Service;
using Crossroad.System.Admin.Service.Impl;
using Crossroad.System.Api;
using Crossroad.Utils.Extensions;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Crossroad.System.Admin.Controller
{
    /// <summary>
    /// 系统角色
    /// </summary>
    [ApiAuthorize]
    [Route("api/system/admin/roles")]
    public class RoleController : BaseApi
    {
        /// <summary>
        /// 系统角色 service
        /// </summary>
        private readonly RoleService roleService;

        public RoleController(
            IRoleService roleServiceIpt
        )
        {
            roleService = roleServiceIpt as RoleService;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="keywords"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        [HttpGet]
        public object GetList(
            int? pageIndex = null,
            int? pageSize = null,
            string keywords = ""
        )
        {
            //无分页列表查询
            if (pageIndex == null && pageSize == null)
                return roleService.GetList(queryable =>
                {
                    queryable
                        .OrderBy(a => a.sort)
                        .OrderBy(a => a.createTime, OrderByType.Desc);

                    if(keywords.Contains())
                        queryable.Where(a => SqlFunc.Contains(a.name, keywords));
                });

            //有分页列表查询
            pageIndex = pageIndex ?? 1;
            pageSize = pageSize ?? AppConfig.DEFAULT_PAGE_SIZE;

            return roleService.GetPaging(
                pageIndex.Value,
                pageSize.Value,
                queryable =>
                {
                    queryable
                        .OrderBy(a => a.sort)
                        .OrderBy(a => a.createTime, OrderByType.Desc);

                    if(keywords.Contains())
                        queryable.Where(a => SqlFunc.Contains(a.name, keywords));
                }
            );
        }

        /// <summary>
        /// 查询详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public object GetDetail(
            int id
        )
        {
            return roleService.GetEntity(id);
        }

        /// <summary>
        /// 查询角色map
        /// </summary>
        /// <returns></returns>
        [Permission("admin/role/index/")]
        [HttpGet("map")]
        public object Maps()
        {
            var list = roleService.GetDicList(queryable => queryable.OrderBy(a => a.sort));

            return list;
        }

        /// <summary>
        /// 查询返回字典类型
        /// </summary>
        /// <returns></returns>
        [HttpGet("dic")]
        public object Dic()
        {
            var list = roleService.GetDicList(queryable => queryable.OrderBy(a => a.sort));

            return list;
        }

        /// <summary>
        /// 新增一个系统角色
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost]
        public object CreateAt(
            [FromBody] Role parameter
        )
        {
            parameter.creatorId = CurrentUser.id;
            return roleService.CreateAt(parameter);
        }

        /// <summary>
        /// 修改指定系统角色
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="id">系统角色的id</param>
        /// <returns></returns>
        [HttpPatch("{id}")]
        public object UpdateAt(
            [FromBody] RoleDto parameter,
            int id
        )
        {
            parameter.id = id;
            parameter.updatorId = CurrentUser.id;

            return roleService.UpdateAt(parameter);
        }

        /// <summary>
        /// 删除一个系统角色
        /// </summary>
        /// <param name="id"></param>
        [HttpDelete]
        [Route("{id}")]
        public object DeleteAt(int id)
        {
            roleService.DeleteAt(new RoleDto
            {
                id = id
            });

            return true;
        }


    }
}
